//
//  LJBaseViewController.swift
//  LJWeibo
//
//  Created by Raymond on 15/12/15.
//  Copyright © 2015年 liu. All rights reserved.
//

import UIKit

class LJBaseViewController: UITableViewController,LJVistorViewDelegate {
    //模拟用用户未登录
    var userLogin = LJUserAccount.isUserLogin
    
    override func loadView() {
        //判断用户是否登录，未登录显示游客界面
        userLogin ? super.loadView() : self.setupVistorView()
    }
    
    

    private func setupVistorView() {
        
        view = vistorView
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: UIBarButtonItemStyle.Plain, target: self, action: "vistorViewDidClickRegisterButton")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: UIBarButtonItemStyle.Plain, target: self, action: "vistorViewDidClickLoginButton")
        
        //判断控制器
        if self is LJHomeViewController{
            vistorView.startAnimation()
            
            // 设置监听
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "didEnterBackground", name: UIApplicationDidEnterBackgroundNotification, object: nil)
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "didBecomeActive", name: UIApplicationDidBecomeActiveNotification, object: nil)
        }else if self is LJMessageViewController{
            vistorView.setupInfo("visitordiscover_image_message", message: "登录后，别人评论你的微博，发给你的消息，都会在这里收到通知")
        }else if self is LJDiscoverViewController{
            vistorView.setupInfo("visitordiscover_image_message", message: "登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过")
        }else if self is LJProfileViewController{
            vistorView.setupInfo("visitordiscover_image_profile", message: "登录后，你的微博、相册、个人资料会显示在这里，展示给别人")
        }
        
    }
    
    deinit{
        if self is LJHomeViewController{
            // 移除通知
            NSNotificationCenter.defaultCenter().removeObserver(self)
        }
        
    }
    
    //MARK: - vistorView代理方法实现
    func vistorViewDidClickLoginButton() {
        // 创建授权控制器
        let OauthCV = LJOauthViewController()
        
        presentViewController(UINavigationController(rootViewController: OauthCV), animated: true, completion: nil)
    }
    func vistorViewDidClickRegisterButton() {
        print(__FUNCTION__)
    }
    
    //MARK: - 通知监听方法
    //进入后台时调用
    @objc private func didEnterBackground(){
        vistorView.pauseAnimation()
    }
    @objc private func didBecomeActive(){
        vistorView.resumeAnimation()
    }
    //MARK: - 懒加载
    lazy var vistorView: LJVistorView = {
        var view = LJVistorView()
        view.delegate = self
        return view
    }();
    
}
